package com.easywork.jythonspring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public abstract class PythonServletUtils {
	/**
	 * Get parameter by specific name from request
	 * @param req HttpServletRequest object
	 * @param paramName Parameter name
	 * @return If the name contains a value, return the value, or null
	 */
	public static String getValue(HttpServletRequest req,String paramName)
	{
		return req.getParameter(paramName);
	}
	
	/**
	 * Get array values method
	 * @param req HttpServletRequest object
	 * @param paramName
	 * @return If the name contains a value array, return the value, or null
	 */
	public static String[] getValues(HttpServletRequest req,String paramName)
	{
		return req.getParameterValues(paramName);
	}
	
	
	/**
	 * Set a value to session scope
	 * @param req HttpServletRequest object
	 * @param name Attribute Name
	 * @param value Object value
	 */
	public static void setToSession(HttpServletRequest req,String name, Object value)
	{
		req.getSession(true).setAttribute(name, value);
	}
	
	/**
	 * Set a value to session scope
	 * @param session HttpSession object
	 * @param name Attribute Name
	 * @param value Object value
	 */
	public static void setToSession(HttpSession session,String name, Object value)
	{
		session.setAttribute(name, value);
	}
	
	/**
	 * Get attribute from session
	 * @param req HttpServletRequest object
	 * @param name Attribute Name
	 * @return Object value in session
	 */
	public static Object getFromSession(HttpServletRequest req,String name)
	{
		HttpSession session = req.getSession(false);
		return session == null ? null : session.getAttribute(name);
	}
	
	/**
	 * Get attribute from session
	 * @param session HttpSession object
	 * @param name Attribute Name
	 * @return Object value in session
	 */
	public static Object getFromSession(HttpSession session,String name)
	{
		return session == null ? null : session.getAttribute(name);
	}
}
